package com.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beans.Company;
import com.beans.CompanyLog;
import com.dao.CompanyDao;

public class CompanyDaoImpl extends UniversalDaoImpl implements CompanyDao {

	public Company login(String name, String pwd,String ip) {
		Company c = null;
		String sql = "from com.beans.Company where loginName=? and pwd=?";
		Object[] p = new Object[] { name, pwd };
		List l = this.getHibernateTemplate().find(sql, p);
		if (l != null && l.size() > 0){
			c = (Company) l.get(0);
			excuteSQL("insert j_company_log(companyID,ip) values("+c.getId()+",'"+ip+"')");
		}
		return c;
	}

	public boolean companyPwd(int userid, String oldPwd, String newPwd) {
		String sql = "update j_company set pwd=?,pwd1=pwd,pwd2=pwd1,pwd3=pwd2 where id=? and pwd=?";
		int t = getJdbcTemplate().update(sql,
				new Object[] { newPwd, userid, oldPwd });
		if (t == 1)
			return true;
		return false;
	}

	public void fix(int userID, int fix) {
		String sql = "update j_company set fix=" + fix + " where id=" + userID;
		excuteSQL(sql);
	}

	public List topFixPosition(int top) {
		String sql = "select top "
				+ top
				+ " id,cName,buildDate,money,employee from j_company  order by fix desc";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public List topNewPosition(int top) {
		String sql = "select top "
				+ top
				+ " id,cName,buildDate,money,employee from j_company  order by registTime desc";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public List topViewPosition(int top) {
		String sql = "select top "
				+ top
				+ " id,cName,buildDate,money,employee from j_company  order by views desc";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	public void addViews(int id) {
		String sql="update j_company set views=views+1 where id="+id;
		excuteSQL(sql);
	}

	public void delete(int id) {
		String sql = "delete j_position where companyID="+id+";delete j_company where id=" + id;
		excuteSQL(sql);
	}

	public com.vo.Page getLog(int pageNo, int pageSize, CompanyLog log) {
		String sql = "select l.id,l.companyID,l.ip,l.createTime,a.loginName,a.cName from j_company_log l,j_company a where a.id=l.companyID";
		Map param=new HashMap();
		if(log.getCompanyID()>0)param.put("l.companyID=?", log.getCompanyID());
		if(log.getLoginName()!=null&&!"".equals(log.getLoginName()))param.put("a.loginName like ?", "%"+log.getLoginName()+"%");
		return Page(pageNo, pageSize, sql,"createTime", param);
	}
}